package com.dl.mall.exception;

import com.dl.mall.utils.result.Result;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局处理库存不足异常
 * @author DL
 * @create 2022-04-07 10:49
 */
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class GlobalNoInventoryException {

    @ResponseBody
    @ExceptionHandler(NoInventoryException.class)
    public Result handlerException(NoInventoryException exception, HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        // 打印堆栈，以供调试
        //exception.printStackTrace();
        System.out.println(exception.getMessage());

        // 返回给前端
        return Result.no(exception.getMessage());
    }

}
